package com.gx.smart.building.ui.swap.viewmodel

import android.app.Application
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.gx.smart.building.admin.ScheduleAppApiService
import com.gx.smart.building.ui.common.viewmodel.CommonViewModel
import com.gx.smart.common.util.AppConfig
import com.gx.smart.lib.http.api.asynctask.CallBack
import com.huishi.gx.ib.grpc.lib.ib.admin.schedule.v1.AppScheduleRecordPlusResp
import com.huishi.gx.ib.grpc.lib.ib.admin.schedule.v1.AppScheduleTime
import com.huishi.gx.ib.grpc.lib.ib.admin.schedule.v1.AppScheduleTimeQueryResp
import com.huishi.gx.ib.grpc.lib.ib.admin.schedule.v1.RecordDto

class ModifySwapViewModel(app: Application) : CommonViewModel(app) {
    private val updateQRCode: MutableLiveData<String> = MutableLiveData()
    private val scheduleTimeList: MutableLiveData<List<AppScheduleTime>> = MutableLiveData()

    //交接班动作——交接单修改（返回成功与否，并附带一个二维码）
    fun updateAppScheduleRecord(content: RecordDto): LiveData<String> {
        ScheduleAppApiService.getInstance().updateAppScheduleRecord(
            content,
            object : CallBack<AppScheduleRecordPlusResp>() {
                override fun callBack(result: AppScheduleRecordPlusResp?) {
                    if (result == null) {
                        ToastUtils.showLong("服务器超时")
                        return
                    }
                    val comResp = result.comResp
                    if (comResp.code == 100) {
                        updateQRCode.value = result.id
                    } else {
                        handleServiceException(comResp)
                    }
                }
            })
        return updateQRCode
    }

    //发起交接——查询该用户，对应班次下的可选择班时（返回对应班时和该班时在该交接班模板下的交接时间范围）
    fun queryAppScheduleTimeListByUser(modelId: String): LiveData<List<AppScheduleTime>> {
        val userId = SPUtils.getInstance().getString(AppConfig.ID)
        ScheduleAppApiService.getInstance().queryAppScheduleTimeListByUser(userId, modelId,
            object : CallBack<AppScheduleTimeQueryResp>() {
                override fun callBack(result: AppScheduleTimeQueryResp?) {
                    if (result == null) {
                        ToastUtils.showLong("服务器超时")
                        return
                    }
                    val comResp = result.comResp
                    if (comResp.code == 100) {
                        scheduleTimeList.value = result.contentList
                    } else {
                        handleServiceException(comResp)
                    }
                }
            })
        return scheduleTimeList
    }
}